package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    //页面初始化分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        //先按照套餐表查询信息并分页
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Setmeal::getName, name);
        wrapper.orderByAsc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, wrapper);
        //分页之后复制到浏览器接收的实体类对象
        Page<SetmealDto> setmealDtoPageInfo = new Page<>();
        BeanUtils.copyProperties(pageInfo, setmealDtoPageInfo, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> returnRecords = records.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            //复制基础属性
            BeanUtils.copyProperties(setmeal, setmealDto);
            //给菜品名称赋值
            setmealDto.setCategoryName(categoryService.getById(setmeal.getCategoryId()).getName());
            //给菜口味赋值
            LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
            setmealDto.setSetmealDishes(setmealDishService.list(wrapper1));
            return setmealDto;

        }).collect(Collectors.toList());
        setmealDtoPageInfo.setRecords(returnRecords);
        return R.success(setmealDtoPageInfo);
    }

    //添加套餐
    /*
    @PostMapping
    public R<String> addSetMeal(@RequestBody SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        setmealService.save(setmeal);
        List<SetmealDto> setmealDishs = setmealDto.getSetmealDishes();
        List<SetmealDto> setmeals = setmealDishs.stream().map(setmeal1 -> {
            setmeal1.setCategoryId(setmeal.getCategoryId());
            return setmeal1;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmeals);
        return R.success("添加成功！！");
    }*/
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    //根据id查询要修改的套餐
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable long id) {

        return setmealService.get(id);
    }

    //根据修改返回的数据对数据库先删后增。实现更改套餐信息
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {

        return setmealService.update(setmealDto);
    }

    //根据id起售，停售
    @PostMapping("/status/{status}")
    public R<String> changeStatus( @PathVariable Integer status,@RequestParam List<Long> ids) {
        return setmealService.changeStatus(status,ids);
    }
    //根据id删除
    @DeleteMapping
    public R<String> removeByIds(@RequestParam("ids")List<Long> ids) {
        return setmealService.removeByIdsMy(ids);}
}
